package org.spring.springboot.app.community.member.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.house.dao.CommunityHouseDao;
import org.spring.springboot.app.community.house.domain.po.CommunityHousePO;
import org.spring.springboot.app.community.member.dao.CommunityHouseUserDao;
import org.spring.springboot.app.community.member.dao.CommunityMemberDao;
import org.spring.springboot.app.community.member.domain.po.CommunityHouseUserPO;
import org.spring.springboot.app.community.member.domain.po.CommunityMemberPO;
import org.spring.springboot.app.community.member.domain.vo.*;
import org.spring.springboot.app.community.member.service.ICommunityMemberService;
import org.spring.springboot.app.community.structure.dao.CommunityStructureDao;
import org.spring.springboot.app.community.structure.dao.CommunityStructureMemberDao;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructurePO;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructureMemberPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CommunityMemberServiceImpl implements ICommunityMemberService{

    @Autowired
    private CommunityMemberDao dao;
    @Autowired
    private CommunityHouseUserDao chudao;
    @Autowired
    private CommunityHouseDao housedao;
    @Autowired
    private CommunityStructureDao communityStructureDao;
    @Autowired
    private CommunityStructureMemberDao communityStructureMemberDao;
    /**
     *
     * @param seachVo
     * @return
     */
    @Override
    public List<CommunityMemberVO> findAll(SeachCommunityMemberVO seachVo){
        if(StringUtils.isNotBlank(seachVo.getOfficeId())&&seachVo.getOfficeId().equals("0")){
            CommunityMemberPO po = new CommunityMemberPO();
            seachVo.setOfficeId(po.getUser().getOfficeId());
        }
        return dao.findAll(seachVo,seachVo.getPageNum(), seachVo.getPageSize());
    }
    /**
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional
    public R save(SaveCommunityMemberVO vo){
        R r = new R();
        if(StringUtils.isBlank(vo.getHouseId())){
            return   r.fail().setMessage("房间ID不能为空");
        }
        CommunityMemberPO po = new CommunityMemberPO();
        BeanUtils.copyProperties(vo, po);
        po.setInfoId(chudao.findInfoIdByHouserId(vo.getHouseId()));
        String pid = dao.queryIdBypo(po);
        if(StringUtils.isBlank(pid)){
            po.setOfficeId(po.getUser().getOfficeId());
            po.setType((byte)1);
            po.setUserId(dao.getUserIdByMobile(vo.getMobile()));
            po.preInsert();
            if(dao.insertSelective(po)<1){
                return   r.fail();
            }
        }else{
            po.setId(pid);
        }
        /**
         * 添加community_structure_member
         */
        CommunityStructurePO hPo= communityStructureDao.selectByPrimaryKey(vo.getHouseId());
        CommunityStructureMemberPO cupp= new CommunityStructureMemberPO();
        cupp.setStructureId(vo.getHouseId());
        cupp.setMemberId(po.getId());
        communityStructureMemberDao.insert(cupp);
        String[] parentIds = hPo.getParentIds().split(",");
        for (String parentId:parentIds) {
            cupp.setStructureId(parentId);
            communityStructureMemberDao.insert(cupp);
        }
        /**
         * 添加用户房间关联表
         */
        CommunityHouseUserPO houseUserPO = new CommunityHouseUserPO();
        houseUserPO.setHouseId(vo.getHouseId());
        houseUserPO.setMemberId(po.getId());
        if(chudao.insertSelective(houseUserPO)<1){
            return   r.fail();
        }
        return   r.success();
    }

    /**
     *
     * @param vo
     * @return
     */
    @Override
    public R update(UpdateCommunityMemberVO vo){
        R r = new R();
        CommunityMemberPO po = new CommunityMemberPO();
        BeanUtils.copyProperties(vo, po);
        if(StringUtils.isBlank(po.getId())){
            return  r.fail().setMessage("ID不能为空");
        }
        if(StringUtils.isNotBlank(vo.getMobile())){
            po.setUserId(dao.getUserIdByMobile(vo.getMobile()));
        }
        po.getUpdateBy();
        if(dao.updateByPrimaryKeySelective(po)<1){
            return   r.fail();
        }
        return   r.success();
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public CommunityMemberPO find(String id){
        CommunityMemberPO po = new CommunityMemberPO();
        return dao.selectByPrimaryKey(id);
    }

    /**
     * 检查楼栋信息并且返回楼栋名称
     * @param buildId
     * @return
     */
    @Override
    public R checkBuildId(String buildId){
        R r = new R();
        String builName = dao.getBuilNameBybuId(buildId);
        if(StringUtils.isBlank(builName)){
            return  r.fail();
        }
        return  r.success().setData(builName);
    }

    /**
     * 根据楼栋ID得到数据
     * @param buildId
     * @return
     */
    @Override
    public  List<ExcelCommunityMemberVO> findExcelAllList(String buildId){
        return dao.findExcelAllList(buildId);
    }

    /**
     *
     * @param list
     * @return
     */
    @Override
    @Transactional
    public  R opExcelCommunityMember(List<ExcelCommunityMemberVO> list){
        R r = new R();
        int addNum = 0;
        int updateNum = 0;
        int deleteNum = 0;
        StringBuilder failureMsg = new StringBuilder();
        for (ExcelCommunityMemberVO vo : list) {
             if(StringUtils.isBlank(vo.getId())){
                 continue;
             }
             if(StringUtils.isBlank(vo.getMonthCost())){
                vo.setMonthCost("0");
             }
            // 更新房间物业费
            CommunityHousePO hpo = new CommunityHousePO();
            hpo.setId(vo.getId());
            hpo.setMonthCost(new BigDecimal(vo.getMonthCost()));
            hpo.preUpdate();
            housedao.updateByPrimaryKeySelective(hpo);
             //通过房间ID得到用户信息
            String memberID=dao.getMemBerByHouse(vo.getId());
            CommunityMemberPO po = new CommunityMemberPO();
            //添加社区成员 通过房间ID没找到社区成员 并且传上来的用户姓名和电话都有的话需要添加一条数据
            if(StringUtils.isBlank(memberID)&&StringUtils.isNotBlank(vo.getUsername())&&StringUtils.isNotBlank(vo.getPhone())){
                po.setInfoId(chudao.findInfoIdByHouserId(vo.getId()));
                po.setMobile(vo.getPhone());
                String pid = dao.queryIdBypo(po);
                if(StringUtils.isBlank(pid)){
                    po.preInsert();
                    po.setName(vo.getUsername());
                    po.setOfficeId(po.getUser().getOfficeId());
                    po.setType((byte)1);
                    po.setState((byte)1);
                    po.setUserId(dao.getUserIdByMobile(po.getMobile()));
                    if(dao.insertSelective(po)<1){
                        throw new RuntimeException();
                    }
                }else{
                    po.setId(pid);
                }
                /**
                 * 添加community_structure_member
                 */
                CommunityStructurePO hPo= communityStructureDao.selectByPrimaryKey(vo.getId());
                CommunityStructureMemberPO cupp= new CommunityStructureMemberPO();
                cupp.setStructureId(vo.getId());
                cupp.setMemberId(po.getId());
                communityStructureMemberDao.insert(cupp);
                String[] parentIds = hPo.getParentIds().split(",");
                for (String parentId:parentIds) {
                    cupp.setStructureId(parentId);
                    communityStructureMemberDao.insert(cupp);
                }
                CommunityHouseUserPO houseUserPO = new CommunityHouseUserPO();
                houseUserPO.setHouseId(vo.getId());
                houseUserPO.setMemberId(po.getId());
                if(chudao.insertSelective(houseUserPO)<1){
                    throw new RuntimeException();
                }
                addNum++;
                continue;
            }
            //修改社区成员  通过房间ID找到了社区成员 并且传上来的用户姓名和电话有一个的的话需要修改
            if(StringUtils.isNotBlank(memberID)&&(StringUtils.isNotBlank(vo.getUsername())||StringUtils.isNotBlank(vo.getPhone()))){
                po.setId(memberID);
                po.setName(vo.getUsername());
                po.setMobile(vo.getPhone());
                po.setUserId(dao.getUserIdByMobile(po.getMobile()));
                po.preUpdate();
                dao.updateByPrimaryKeySelective(po);
                updateNum++;
                continue;
            }
            //删除社区成员  没经过上面添加修改的操作就需要删除
            if(StringUtils.isNotBlank(memberID)){
                CommunityHouseUserPO houseUserPO = new CommunityHouseUserPO();
                houseUserPO.setHouseId(vo.getId());
                houseUserPO.setMemberId(memberID);
                chudao.deleteByPrimaryKey(houseUserPO);
                deleteNum++;
                continue;
            }
        }
        r.setData("总共处理了"+list.size()+"条数据 : 添加了"+addNum+"个业主，"+"修改了"+updateNum+"个业主，"+"删除了"+deleteNum+"个业主");
        return  r.success();
    }
}
